The Tomcat Servlet Container

 

 

0.     You may be able to run Tomcat on your p drive.  I’ll try to check this out the first week of classes.

1.    About Tomcat

         Tomcat is open-source and part of the Apache projects. Tomcat 6X is located at http://tomcat.apache.org/download-60.cgi

         A “servlet container” is like a mini server, but only for serving html, jsp and servlets.

         Many servlet containers could be used for this course. Some may even be easier to configure than tomcat, but tomcat provides an easy-to-use development/deployment tool and also complies with the servlet specification best of all containers. Some of the examples posted may have been deployed in Glassfish or JBoss.  JBoss is a Tomcat server and Glassfish – from Sun - is the server bundled with Netbeans.

         Download the appropriate zip and extract the files to a directory named, for example, jakarta-tomcat-5.0.28 (or whatever the version is)

         The Core Servlets text maintains a site with tips for configuring different versions of Tomcat at http://www.coreservlets.com. This site has a preconfigured version of Tomcat which I have also used.

         Tomcat can be used as a stand-alone servlet container.

         You can install the Apache server with Tomcat, and then proceed to configure each for their individual purposes. (The server would relay servlet requests to Tomcat. This is a non-trivial activity.)

         Tomcat 5.5 does not require editing classpath or definition of JAVA_HOME or CATALINA_HOME. It has windows installer download and a GUI interface for startup/shutdown:

The blackscreen associated with Tomcat shows webapps as they deploy and will also show any error messages as webapps are deployed or run.

 

 

2.     Aside on XML

·        XML stands for eXtensible-markup-language and is a SGML.

·        Unlike older HTML, XML can be validated.

·        XHTML, for example, is HTML that is XML compliant.

·        XML is widely used in internet programming. Some technologies - like SOAP - are based on XML.

·        Many files on Tomcat are XML.

·        In the past I’ve taught XML as a full component of this course. I am not doing that this semester. There are websites with tutorials.  I have links to a whole text of xml content at http://employees.oneonta.edu/higgindm/internet%20programming/xmllinks.html

3.     The Servlet API

·        Links to servlet API are on the text’s web site. 

·        The servlet-api.jar file should be in Tomcat somewhere or can be downloaded.

·        You should bookmark the servlet API for reference purposes: http://tomcat.apache.org/tomcat-5.5-doc/servletapi/  Here is another API documentation link: http://java.sun.com/products/servlet/2.2/javadoc/

·        The servlet class files also need to be on your classpath. These are part of java EE but not part of java SE. I downloaded them and put mine in a c:\servlets directory.  You may instead include the jar file “copy” from the Tomcat server’s library on your classpath. This would look something like

·        C:\tomcatdir\common\lib\servlet.jar

·        The servlet API does not allow inclusion of any standard Java classes in the lib directory of a webapp, this means, for example, that you can’t put the servlet jar file in your lib directory. Tomcat will throw it out when it is deployed.

 

4.     Apache Tomcat Native Library

 

·        You may get an error that the Apache Portable Runtime (APR) is missing. Some older versions of Tomcat used to have this problem.

·        Tomcat will run ok without it.

·        To eliminate this warning, you need to get a dll file for the appropriate tomcat version (mine wanted tcnative.dll v1.8) and put it into C:/your-tomcat/bin and make sure this dir is on the path settings to get rid of the error.

·        More suggestions are at: http://tomcat.apache.org/native-doc/

5.     The Root directory

·        Under the  jakarta-tomcat-v.?.? is webapps.  In this directory appears a directory called ROOT. The servlets deployed in ROOT are recognized by the server without further path information or modification to any web.xml file.

·        It is a good idea to deploy servlets elsewhere.

6.     Sample web.xml. (You may need to go look at notes on xml.  An xml is a text file, may have a schema associated with it specifying syntax- web.xml does, for example. Among other things, xml is case sensitive.) web.xml is a special xml file that contains deployment information for your servlet… Several of the tags below are optional, and some important options are not shown.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

   version="2.4">

 

   <!-- General description of your Web application -->

   <display-name>

      Java How to Program JSP

      and Servlet Chapter Examples

   </display-name>

 

   <description>

      This is the Web application in which we

      demonstrate our JSP and Servlet examples.

   </description>

<!-- Servlet definitions -->

   <servlet>

      <servlet-name>welcome1</servlet-name>

 

      <description>

         A simple servlet that handles an HTTP get request.

      </description>

 

      <servlet-class>

         WelcomeServlet

      </servlet-class>

   </servlet>

<servlet>

      <servlet-name>welcome2</servlet-name>

<description>

         A simple servlet that handles an HTTP get request with data.

      </description>

 

      <servlet-class>

         WelcomeServlet2

      </servlet-class>

   </servlet>

   <!-- Servlet mappings -->

   <servlet-mapping>

      <servlet-name>welcome1</servlet-name>

      <url-pattern>/welcome1</url-pattern>

 </servlet-mapping>

<servlet-mapping>

     <servlet-name>welcome2</servlet-name>

      <url-pattern>/welcome2</url-pattern>

</web-app>

 

 

7.     Errors:  Even if your servlet compiles you may have deployment, runtime, or dependency errors.  You’ll have to watch the blackscreen associated with your server session for errors on startup or reloads. If these occur you may have to fix them before your servlet can be accessed. The Tomcat log (in TomcatHome\logs) files also contain information on exceptions.  Check log file dates to see which is relevant.

8.     Tomcat admin link should be here.

 

9.     Classpaths & Environment variables

 

10.                        Testing Tomcat installation

11.                          Tomcat admin

You can drop war files or webapp directories into your tomcat/webapps folder and they will deploy.  Undeployment is trickier, since information about webapps is also saved in C:\tomcat\work\Catalina\localhost.  To undeploy a webapp cleanly it is better to use the management tool.  First, check what users are available in the tomcat/conf/tomcat-users.xml.

Launch your favorite editor and if need be, add a user with manager/admin privileges to $CATALINA_HOME/conf/tomcat-users.xml.

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
  <user username="TomcatAdmin" password="tcpass" roles="admin,manager"/>
</tomcat-users>

 

.  On the left side of the tomcat screen is a link for the manager.  

 

 

The manager lists all deployed applications and can be used to deploy/undeploy applications.